﻿using System;
using System.Data;
using BO.Infrastructure.Utils;
using BO.News;
using BO.Infrastructure.StaticData;
using GUI.Extensions;
using ChannelVN.Cached;

namespace GUI.Controls.Sport
{
    public partial class NewsZone : System.Web.UI.UserControl
    {
        public int ZoneId { get; set; }
        public bool GetDataFromNoCacheLayer = false;

        private const string TopItemFormat = "{3}<div class=\"text fl\"><h3><a title=\"{2}\" href=\"{0}\">{1}{7}</a></h3><p class=\"tacgia\">{4} | <span>{5}</span></p><p>{6}</p></div>";
        private const string LiFormat = "<li><span></span><a title=\"{0}\" href=\"{1}\">{2}</a></li>";
        private const string LinkFormat = "<a href=\"{0}\" title=\"{1}\">{1}</a>";
        private const int ImageWidth = 200;
        private const int ImageHeight = 150;
        private const int TopHighlight = 1;
        private const int TopOtherNews = 3;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ZoneId > 0)
            {
                var objCat = NewsBO.GetInstance().GetCategoryDetail(ZoneId);
                if (objCat != null)
                {
                    ltrTitle.Text = string.Format(LinkFormat, objCat.Url, objCat.Name);

                    var enableHtmlCached = GetDataFromNoCacheLayer && CacheSettingExtensions.EnableCacheHtml;
                    var bo = enableHtmlCached ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();
                    var isCachedUpdating = enableHtmlCached && CacheController.IsCachedUpdating;
                    #region TopItem
                    {
                        var dt = bo.GetSportNewsByPosition(top: TopHighlight, typeId: DefinedConstants.SportNews.TypeIds.NoiBatChuyenMuc, zoneId: ZoneId, getDataFromMasterDataBase: isCachedUpdating);

                        if (dt != null && dt.Rows.Count > 0)
                        {
                            var r = dt.Rows[0];
                            var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth, ImageHeight, lazyLoad: true);

                            ltrTop.Text = string.Format(TopItemFormat,
                                r[SqlConstants.NewsPublish.Url],
                                r[SqlConstants.NewsPublish.Title],
                                PresentationUtils.EnCode(r[SqlConstants.NewsPublish.Title].ToString()),
                                avatar,
                                r[SqlConstants.NewsPublish.Author],
                                PresentationUtils.Object2DateTime(r[SqlConstants.NewsPublish.DistributionDate].ToString()).ToString("dd/MM/yyyy HH:mm"),
                                r[SqlConstants.NewsPublish.InitSapo],
                                r[SqlConstants.NewsPublish.NewsTypeIcon]);
                        }
                    }
                    #endregion

                    #region OtherItems
                    {
                        var dt = bo.GetSportNewsByPosition(top: TopOtherNews, typeId: DefinedConstants.SportNews.TypeIds.TinTrangChuyenMuc, zoneId: ZoneId, getDataFromMasterDataBase: isCachedUpdating);

                        if (dt != null && dt.Rows.Count > 0)
                        {
                            var sb = new System.Text.StringBuilder();
                            foreach (DataRow r in dt.Rows)
                            {
                                sb.AppendFormat(LiFormat,
                                                       r[SqlConstants.NewsPublish.EncodedTitle],
                                                       r[SqlConstants.NewsPublish.Url],
                                                       r[SqlConstants.NewsPublish.Title]
                                                       );
                            }
                            ltrList.Text = sb.ToString();
                        }

                    }
                    #endregion

                    return;
                }
            }
            Visible = false;

        }
    }
}